package com.thrd.taiexhibition.fragement;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieListener;
import com.alibaba.android.arouter.launcher.ARouter;
import com.thrd.basicframework.bean.BaseFragment;
import com.thrd.basicframework.ui.defaultview.LoadingState;
import com.thrd.taiexhibition.BaseApplication;
import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.adapter.CityFocusAdapter;
import com.thrd.taiexhibition.bean.BeanCityIndustry;
import com.thrd.taiexhibition.bean.BeanCityIndustryFocus;
import com.thrd.taiexhibition.bean.CityEvent;
import com.thrd.taiexhibition.fresco.FrescoImage;
import com.thrd.taiexhibition.fresco.FrescoUtils;
import com.thrd.taiexhibition.presener.CityFocusPresener;
import com.thrd.taiexhibition.ui.FocusIndustryView;
import com.thrd.taiexhibition.utils.Constant;
import com.thrd.taiexhibition.view.CityFocusView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * 重点长野
 * @author: ${唐杰}
 * @date: 2021/3/22
 */
public class CityFocusFragment extends BaseFragment implements CityFocusView, FocusIndustryView.OnItemImageListener {

    private static final String TAG="CityFocusFragment";
    private RecyclerView mRecyclerView;
    private CityFocusAdapter mCityFocusAdapter;
    private CityFocusPresener mCityFocusPresener;
    private FrescoImage mFrescoImage;
    private LottieAnimationView mLottieAnimationView;
    private TextView mTitleName;
    private TextView mEngLishName;
    private TextView mContent;
    private RelativeLayout mLeftLayout;

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_focus_industry;
    }

    @Override
    protected void setUpView(View mContentView) {
        mRecyclerView = mContentView.findViewById(R.id.rv_recyclerview);
        mFrescoImage = mContentView.findViewById(R.id.fi_image);
        mLottieAnimationView = mContentView.findViewById(R.id.lottie_view);
        mTitleName = mContentView.findViewById(R.id.tv_title_name);
        mEngLishName = mContentView.findViewById(R.id.tv_english_name);
        mContent = mContentView.findViewById(R.id.tv_content);
        mLeftLayout = mContentView.findViewById(R.id.rl_left_layout);
        LinearLayoutManager manager=new LinearLayoutManager(getMContext());
        manager.setOrientation(RecyclerView.VERTICAL);
        PagerSnapHelper scrollHelper = new PagerSnapHelper();
        scrollHelper.attachToRecyclerView(mRecyclerView);
        mRecyclerView.setLayoutManager(manager);
    }

    @Override
    protected void setUpData() {
        mCityFocusPresener=new CityFocusPresener(this);
        mCityFocusAdapter = new CityFocusAdapter(getMContext(),cityIndustryList,this);
        mRecyclerView.setAdapter(mCityFocusAdapter);




        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //滑动停止
                if(RecyclerView.SCROLL_STATE_IDLE==newState){
                    LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    int position=manager.findFirstVisibleItemPosition();
                    if(position>-1&&manager.findViewByPosition(position) instanceof FocusIndustryView){
                        //第一个可见的位置的view
                        FocusIndustryView mFocusIndustryView = (FocusIndustryView) manager.findViewByPosition(position);
                        if(mFocusIndustryView!=null){
                            mFocusIndustryView.setAnimention();
                        }
                    }

                }
            }
        });

        mLeftLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemImageListener(mBeanCityIndustryFocus);
            }
        });

    }

    @Override
    protected void onRecoveryBundle(Bundle savedInstanceState) {
        Log.e(TAG,"onRecoveryBundle");
        if(savedInstanceState!=null){
            mBeanCityIndustryList= (BeanCityIndustry) savedInstanceState.getSerializable("BeanCityIndustryList");
            onSuccess(mBeanCityIndustryList);
        }
        super.onRecoveryBundle(savedInstanceState);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {

        Log.e(TAG,"onSaveInstanceState");
        outState.putSerializable("BeanCityIndustryList", mBeanCityIndustryList);
        super.onSaveInstanceState(outState);
    }



    @Override
    public void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
        BaseApplication mBaseApplication = (BaseApplication) getActivity().getApplication();
        if(mCityFocusPresener!=null&&mBeanCityIndustryList==null&&mBaseApplication!=null){
            mCityFocusPresener.getCityFocusData(mBaseApplication.getCityId());
        }

    }

    @Override
    public void onFragmentResume() {
        super.onFragmentResume();
        if(mLottieAnimationView!=null){
            mLottieAnimationView.playAnimation();
            // mLottieAnimationView.pauseAnimation();
        }
    }

    @Override
    public void onFragmentPause() {
        super.onFragmentPause();
        if(mLottieAnimationView!=null){
            mLottieAnimationView.setProgress(0);
            mLottieAnimationView.pauseAnimation();
        }
    }

    @Override
    public void onError(String error, int type) {
        getContentView().setState(LoadingState.STATE_ERROR);
    }
    private BeanCityIndustry mBeanCityIndustryList;
    private List<List<BeanCityIndustryFocus>> cityIndustryList;
    private BeanCityIndustryFocus mBeanCityIndustryFocus;
    @Override
    public void onSuccess(BeanCityIndustry mBeanCityIndustryList) {
        this.mBeanCityIndustryList=mBeanCityIndustryList;
        if(mBeanCityIndustryList!=null){
            this.cityIndustryList=mBeanCityIndustryList.getCityIndustryListList();
            FrescoUtils.getInstance().setPrefixProgressLoad(getMContext(),mFrescoImage,mBeanCityIndustryList.getIndustryBackImg());
            mBeanCityIndustryFocus= mBeanCityIndustryList.getCtIndustry();
            if(mBeanCityIndustryFocus!=null){
                mTitleName.setText(mBeanCityIndustryFocus.getIndustryName());
                mEngLishName .setText(mBeanCityIndustryFocus.getIndustryEnglishName());
                mContent .setText(mBeanCityIndustryFocus.getIndustryDescribe());

                // mLottieAnimationView.setAnimation(Constant.IMAGE+mBeanCityIndustryFocus.getIndustryDetailBackResource());
                mLottieAnimationView.setAnimationFromUrl(Constant.IMAGE+mBeanCityIndustryFocus.getIndustryDetailBackResource());
                mLottieAnimationView.setFailureListener(new LottieListener<Throwable>() {
                    @Override
                    public void onResult(Throwable result) {
                        result.printStackTrace();
                    }
                });
                mLottieAnimationView.playAnimation();
            }
        }


        mCityFocusAdapter.refresh(cityIndustryList);

        getContentView().setState(LoadingState.STATE_SUCCESS);
    }




    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void switchCity(CityEvent mCityEvent){
        if(mCityEvent!=null&&mCityFocusPresener!=null){
            mBeanCityIndustryList=null;
            mCityFocusPresener.getCityFocusData((int) mCityEvent.getCityId());
        }

    }

    @Override
    public void onItemImageListener(BeanCityIndustryFocus mBeanCityIndustryFocus) {
        if(mBeanCityIndustryFocus!=null){
            ARouter.getInstance().build("/app/fiia")
                    .withString("image",mBeanCityIndustryFocus.getIndustryDetailBackResource())
                    .withLong("id",mBeanCityIndustryFocus.getId())
                    .withInt("industryDetailBackType",mBeanCityIndustryFocus.getIndustryDetailBackType())
                    .navigation();
        }

    }
}
